package com.ls.netty.peotocoltcp.coder;

import com.ls.netty.peotocoltcp.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

public class MyMessageDecoder extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecoder decode 被调用");
        int i = in.readInt();
        byte[] content = new byte[i];
        in.readBytes(content);

        MessageProtocol messageProtocol = new MessageProtocol(i,content);
        out.add(messageProtocol);

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("error");
        ctx.close();
    }
}
